//
//  ChangePassWordViewController.m
//  一点互助
//
//  Created by 张泊秋 on 16/8/27.
//  Copyright © 2016年 zhangboqiu. All rights reserved.
//

#import "ChangePassWordViewController.h"
#import <Masonry/Masonry.h>
#import "ResponseManager.h"
#import "FBProgressView.h"


@interface ChangePassWordViewController ()
{
    UITextField *oldPassWord;
    UITextField *newPassWord;
    UITextField *affirmNewPassWord;
    UIButton *enterButton;
}

@end

@implementation ChangePassWordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = RGBCOLOR(241.0f, 241.0f, 241.0f, 1);
    [self setupBackItem];
    [self setUpView];
    self.title = @"修改密码";
}

- (void)setUpView {
    
    oldPassWord = [self memberTextFieldText:@"请输入旧密码" image:nil label:@"旧密码"];
    [self.view addSubview:oldPassWord];
    
    [oldPassWord mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(72);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(50);
        
    }];
    
    newPassWord = [self memberTextFieldText:@"请设置新密码" image:nil label:@"新密码"];
    [self.view addSubview:newPassWord];
    
    [newPassWord mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(oldPassWord.mas_bottom).with.offset(8);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(50);
        
    }];
    
    affirmNewPassWord = [self memberTextFieldText:@"请确认新密码" image:nil label:@"确认新密码"];
    [self.view addSubview:affirmNewPassWord];
    
    [affirmNewPassWord mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(newPassWord.mas_bottom).with.offset(1);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(50);
        
    }];
    
    
    enterButton  = [UIButton buttonWithType:UIButtonTypeCustom];
    enterButton.backgroundColor = BLUE_COLOR;
    [enterButton setTitle:@"确认修改" forState:UIControlStateNormal];
    [enterButton setTitleColor:RGBCOLOR(205.0f, 205.0f, 205.0f, 1) forState:UIControlStateHighlighted];
    enterButton.titleLabel.textColor = [UIColor whiteColor];
    enterButton.titleLabel.font = [UIFont systemFontOfSize:15];
    enterButton.layer.cornerRadius = 5;
    [self.view addSubview:enterButton];
    
    [enterButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(affirmNewPassWord.mas_bottom).with.offset(30);
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(47);
        
    }];
        
    [enterButton addTarget:self action:@selector(changePasswordButton) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)changePasswordButton {
    
    [FBProgressView showWithMessage:nil];
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSDictionary *dic = [userDefaults objectForKey:@"userInfo"];
    if ([oldPassWord.text isEqualToString:dic[@"password"]]) {
        if ([newPassWord.text isEqualToString:affirmNewPassWord.text] ) {
            NSMutableDictionary *passwordDic = [NSMutableDictionary dictionary];
            passwordDic[@"user_pass"] = newPassWord.text;
            [ResponseManager startPostRequestWithURLString:kamendUserInfoURL parameters:passwordDic completionHandler:^(FBResponse *response) {
                [FBProgressView hide];
                if (response.success) {
                    
                    [self.navigationController popViewControllerAnimated:YES];
                    
                }else {
                    [self addAlertControllerWithTitle:@"提示" message:response.resultDesc actionNum:1 style:UIAlertControllerStyleAlert handler:nil];
                }
                
            }];
            
        }else {
            [self addAlertControllerWithTitle:@"提示" message:@"两次输入不一样!" actionNum:1 style:UIAlertControllerStyleAlert handler:nil];
        }
        
    }else {
        
         [self addAlertControllerWithTitle:@"提示" message:@"原密码错误!" actionNum:1 style:UIAlertControllerStyleAlert handler:nil];
       
    }
    
    
}


@end
